package com.store.app.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.imlife.utils.ToastTools;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.SettingData;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by Ben
 * on 2016/8/26 0026.\
 * 折扣页面
 */
public class DeductionActivity extends BaseActivity implements IOnHttpListener, View.OnClickListener {

    private ImageView public_ll_return;
    private TextView tvTitle;
    private EditText et_1;
    private EditText et_1_2;
    private EditText et_2;
    private EditText et_2_2;
    private EditText et3;
    private Button btn_deduction_save;
    private NetRequest netRequest;
    private CheckBox cb_reward;                 //满送
    private CheckBox cb_rebate;                 //满返
    private CheckBox cb_discount;               //折扣
    String rewardStatus = "disable";
    String rebateStatus = "disable";
    String discountStatus = "disable";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_deduction);
        initView();
        refreshUI();
        netRequest = new NetRequest(this);
        netRequest.requestStoreActivityPaymentFind(2);
//
        Log.v("zyl", "SettingData.moreRebate:" + SettingData.moreRebate);
        Log.v("zyl", "SettingData.moreRebateData:" + SettingData.moreRebateData);
        Log.v("zyl", "SettingData.moreReward:" + SettingData.moreReward);
        Log.v("zyl", "SettingData.moreRewardData:" + SettingData.moreRewardData);
        Log.v("zyl", "SettingData.discount:" + SettingData.discount);
        Log.v("zyl", "SettingData.rebateStatus:" + SettingData.rebateStatus);
        Log.v("zyl", "SettingData.rewardStatus:" + SettingData.rewardStatus);
        Log.v("zyl", "SettingData.discountStatus:" + SettingData.discountStatus);
    }

    @Override
    public void onAfinalFail() {

    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId == 1){
            ToastTools.showShort(DeductionActivity.this, "提交成功");
            netRequest.requestStoreActivityPaymentFind(2);
            finish();
        }else if(requestId == 2){
            try{
                JSONArray jsonArray = new JSONArray(result);
                for(int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String activity = jsonObject.getString("activity");
                    //满返
                    if(activity.equals("rebate")){
                        SettingData.moreRebate = jsonObject.getString("amount");
                        SettingData.moreRebateData = jsonObject.getString("promotion");
                        SettingData.rebateStatus = jsonObject.getString("status");
                    }
                    //折扣
                    if(activity.equals("discount")){
                        SettingData.discount = jsonObject.getString("promotion");
                        SettingData.discountStatus = jsonObject.getString("status");
                    }
                    //满送
                    if(activity.equals("reward")){
                        SettingData.moreReward = jsonObject.getString("amount");
                        SettingData.moreRewardData = jsonObject.getString("promotion");
                        SettingData.rewardStatus = jsonObject.getString("status");
                    }
                }
            }catch (Exception e){}
            refreshUI();
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        ToastTools.showShort(DeductionActivity.this,msg);
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("会员专享");//折扣==>会员专享
        et_1 = (EditText) findViewById(R.id.et_1);
        et_1_2 = (EditText) findViewById(R.id.et_1_2);
        et_2 = (EditText) findViewById(R.id.et_2);
        et_2_2 = (EditText) findViewById(R.id.et_2_2);
        et3 = (EditText) findViewById(R.id.et3);
        btn_deduction_save = (Button) findViewById(R.id.btn_deduction_save);
        btn_deduction_save.setOnClickListener(this);
        cb_reward = (CheckBox) findViewById(R.id.cb_reward);
        cb_rebate = (CheckBox) findViewById(R.id.cb_rebate);
        cb_discount = (CheckBox) findViewById(R.id.cb_discount);



//        cb_reward.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    et_1.setEnabled(true);
//                    et_1_2.setEnabled(true);
//                } else {
//                    et_1.setEnabled(false);
//                    et_1_2.setEnabled(false);
//                }
//            }
//        });
//        cb_rebate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    et_2.setEnabled(true);
//                    et_2_2.setEnabled(true);
//                } else {
//                    et_2.setEnabled(false);
//                    et_2_2.setEnabled(false);
//                }
//            }
//        });
//        cb_discount.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (isChecked) {
//                    et3.setEnabled(true);
//                } else {
//                    et3.setEnabled(false);
//                }
//            }
//        });
    }

    private void refreshUI() {
        //满送活动是否有效
        if (SettingData.rewardStatus.equals("enable")) {
            cb_reward.setChecked(true);
        } else {
            cb_reward.setChecked(false);
        }
        //满返活动是否有效
        if (SettingData.rebateStatus.equals("enable")) {
            cb_rebate.setChecked(true);
        } else {
            cb_rebate.setChecked(false);
        }
        //折扣活动是否有效
        if (SettingData.discountStatus.equals("enable")) {
            cb_discount.setChecked(true);
        } else {
            cb_discount.setChecked(false);
        }
//        //根据活动状态设置能否编辑
//        if (cb_reward.isChecked()) {
//            et_1.setEnabled(true);
//            et_1_2.setEnabled(true);
//        } else {
//            et_1.setEnabled(false);
//            et_1_2.setEnabled(false);
//        }
//        if (cb_rebate.isChecked()) {
//            et_2.setEnabled(true);
//            et_2_2.setEnabled(true);
//        } else {
//            et_2.setEnabled(false);
//            et_2_2.setEnabled(false);
//        }
//        if (cb_discount.isChecked()){
//            et3.setEnabled(true);
//        } else {
//            et3.setEnabled(false);
//        }
        //显示数值
        et_1.setText(SettingData.moreReward);
        et_1_2.setText(SettingData.moreRewardData);
        et_2.setText(SettingData.moreRebate);
        et_2_2.setText(SettingData.moreRebateData);
        et3.setText(SettingData.discount);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_deduction_save:
                submit();
                break;
        }
    }

    public void submit(){





        //满送规则
        if(cb_reward.isChecked()){
            rewardStatus = "enable";
            if(TextUtils.isEmpty(et_1.getText().toString().trim())||TextUtils.isEmpty(et_1_2.getText().toString().trim())){
                ToastTools.showShort(DeductionActivity.this,"请输入满送规则");
                return;
            }
            int moreReward = Integer.parseInt(et_1.getText().toString().trim());
            int moreRewardData = Integer.parseInt(et_1_2.getText().toString().trim());
            if(moreRewardData>moreReward){
                ToastTools.showShort(DeductionActivity.this,"赠送金币比例不能大于1：1");
                return;
            }
        }else{
            rewardStatus = "disable";
        }

        //满返规则
        if(cb_rebate.isChecked()){
            rebateStatus = "enable";
            if(TextUtils.isEmpty(et_2.getText().toString().trim())||TextUtils.isEmpty(et_2_2.getText().toString().trim())){
                ToastTools.showShort(DeductionActivity.this,"请输入满返规则");
                return;
            }
            int moreRebate = Integer.parseInt(et_2.getText().toString().trim());
            int moreRebateData = Integer.parseInt(et_2_2.getText().toString().trim());
            if(moreRebateData>moreRebate){
                ToastTools.showShort(DeductionActivity.this,"返现金额不能大于1：1");
                return;
            }
        }else{
            rebateStatus = "disable";
        }

        //折扣规则

        if(cb_discount.isChecked()){
            discountStatus = "enable";
            if(TextUtils.isEmpty(et3.getText().toString().trim())){
                ToastTools.showShort(DeductionActivity.this,"请输入百分比折扣");
                return;
            }
            int discount = Integer.parseInt(et3.getText().toString().trim());
            if(discount>100){
                ToastTools.showShort(DeductionActivity.this,"折扣不能大于100");
                return;
            }
        }else{
            discountStatus = "disable";
        }

        try{
            JSONObject rewardObj = new JSONObject();
            rewardObj.put("activity", "reward");
            rewardObj.put("stores_id", MainActivity.member_id);
            if(TextUtils.isEmpty(et_1.getText().toString().trim()+"")){
                rewardObj.put("amount","0");
            }
            else{
                rewardObj.put("amount",et_1.getText().toString().trim()+"");
            }
            if(TextUtils.isEmpty(et_1_2.getText().toString().trim()+"")){
                rewardObj.put("promotion","0");
            }
            else{
                rewardObj.put("promotion",et_1_2.getText().toString().trim()+"");
            }
            rewardObj.put("status", rewardStatus);
            rewardObj.put("remark", "送金币");
            JSONObject rebateObj = new JSONObject();
            rebateObj.put("activity", "rebate");
            rebateObj.put("stores_id", MainActivity.member_id);
            if(TextUtils.isEmpty(et_2.getText().toString().trim()+"")){
                rebateObj.put("amount","0");
            }
            else{
                rebateObj.put("amount",et_2.getText().toString().trim()+"");
            }
            if(TextUtils.isEmpty(et_2_2.getText().toString().trim()+"")){
                rebateObj.put("promotion","0");
            }
            else{
                rebateObj.put("promotion",et_2_2.getText().toString().trim()+"");
            }
            rebateObj.put("status", rebateStatus);
            rebateObj.put("remark", "返现金");
            JSONObject discountObj = new JSONObject();
            discountObj.put("activity", "discount");
            discountObj.put("stores_id", MainActivity.member_id);
            discountObj.put("amount","1");
            if(TextUtils.isEmpty(et3.getText().toString().trim()+"")){
                discountObj.put("promotion","0");
            }
            else{
                discountObj.put("promotion",et3.getText().toString().trim()+"");
            }
            discountObj.put("status", discountStatus);
            discountObj.put("remark", "折扣");
            netRequest.requestStoreActivityPaymentEdit(1,rewardObj,rebateObj,discountObj);

        }catch (Exception e){}

    }
}
